<div class="content-body">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 搜索栏 -->
            <div class="layui-form" style="padding-bottom: 10px;">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">卡号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="card_no" placeholder="请输入会员卡号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">全部状态</option>
                                <option value="0">未使用</option>
                                <option value="1">已使用</option>
                                <option value="2">已作废</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" lay-submit lay-filter="LAY-card-search">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="LAY-card-reset">重置</button>
                    </div>
                </div>
            </div>

            <!-- 工具栏 -->
            <div class="layui-btn-group" style="padding-bottom: 10px;">
                <button class="layui-btn" data-type="generate"><i class="layui-icon layui-icon-add-1"></i>生成会员卡</button>
                <button class="layui-btn layui-btn-normal" data-type="export"><i class="layui-icon layui-icon-export"></i>导出</button>
<!--                <button class="layui-btn layui-btn-danger" data-type="batchInvalid"><i class="layui-icon layui-icon-close"></i>批量作废</button>-->
            </div>

            <!-- 数据表格 -->
            <table id="LAY-card-list" lay-filter="LAY-card-list"></table>
        </div>
    </div>
</div>

<!-- 表格操作列模板 -->
<script type="text/html" id="table-card-operation">
    {{# if(d.status == 0){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="invalid"><i class="layui-icon layui-icon-close"></i>作废</a>
    {{# } }}
</script>

<!-- 表格状态列模板 -->
<script type="text/html" id="table-card-status">
    {{# if(d.status == 0){ }}
    <span class="layui-badge layui-bg-blue">未使用</span>
    {{# }else if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">已使用</span>
    {{# }else if(d.status == 2){ }}
    <span class="layui-badge layui-bg-gray">已作废</span>
    {{# } }}
</script>

<script>
layui.use(['table', 'form', 'layer'], function(){
    var $ = layui.$
    ,table = layui.table
    ,form = layui.form
    ,layer = layui.layer;
    
    //会员卡列表
    table.render({
        elem: '#LAY-card-list'
        ,url: '/app/ky_shouyin/admin/member/cardList'
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'card_no', title: '卡号', width: 150}
            ,{field: 'member_name', title: '使用会员', width: 120}
            ,{field: 'status', title: '状态', width: 100, templet: '#table-card-status'}
            ,{field: 'created_at', title: '生成时间', width: 160}
            ,{field: 'used_at', title: '使用时间', width: 160}
            ,{title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#table-card-operation'}
        ]]
        ,page: true
        ,limit: 15
        ,text: {
            none: '暂无数据'
        }
    });
    
    //监听搜索
    form.on('submit(LAY-card-search)', function(data){
        var field = data.field;
        
        //执行重载
        table.reload('LAY-card-list', {
            where: field
        });
    });
    
    //监听重置
    form.on('submit(LAY-card-reset)', function(data){
        //重置表单
        $('input[name="card_no"]').val('');
        $('select[name="status"]').val('');
        form.render('select');
        
        //重载表格
        table.reload('LAY-card-list', {
            where: {}
        });
    });
    
    //监听工具条
    table.on('tool(LAY-card-list)', function(obj){
        var data = obj.data;
        if(obj.event === 'invalid'){
            layer.confirm('确定作废该会员卡？', function(index){
                $.ajax({
                    url: '/app/ky_shouyin/admin/member/voidCard'
                    ,type: 'POST'
                    ,data: {id: data.id}
                    ,success: function(res){
                        if(res.code === 0){
                            layer.msg('作废成功', {icon: 1});
                            obj.update({
                                status: 2
                            });
                        } else {
                            layer.msg(res.msg || '作废失败', {icon: 2});
                        }
                    }
                });
                layer.close(index);
            });
        }
    });

    var active = {
        generate: function(){
            layer.prompt({
                formType: 0,
                value: 'VIP',
                title: '请输入卡号前缀',
                area: ['300px', '150px']
            }, function(value, index, elem){
                layer.close(index);
                layer.prompt({
                    formType: 0,
                    value: '100',
                    title: '请输入生成数量',
                    area: ['300px', '150px']
                }, function(count, index2, elem2){
                    layer.close(index2);
                    
                    // 显示loading
                    var loadIndex = layer.load(2);
                    
                    // 发送请求生成卡号
                    $.ajax({
                        url: '/app/ky_shouyin/admin/member/generateCards',
                        type: 'POST',
                        data: {
                            prefix: value,
                            count: count
                        },
                        success: function(res){
                            layer.close(loadIndex);
                            if(res.code === 0){
                                layer.msg('生成成功，共生成' + res.data.total + '张会员卡', {
                                    icon: 1,
                                    time: 2000
                                });
                                table.reload('LAY-card-list');
                            } else {
                                layer.msg(res.msg || '生成失败', {
                                    icon: 2,
                                    time: 2000
                                });
                            }
                        },
                        error: function(){
                            layer.close(loadIndex);
                            layer.msg('生成失败，请重试', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                });
            });
        },
        export: function(){
            var checkStatus = table.checkStatus('LAY-card-list');
            var ids = checkStatus.data.map(function(item){
                return item.id;
            });
            
            window.location.href = '/app/ky_shouyin/admin/member/exportCards?ids=' + ids.join(',');
        },
        batchInvalid: function(){
            var checkStatus = table.checkStatus('LAY-card-list');
            if(checkStatus.data.length === 0){
                return layer.msg('请选择要作废的会员卡', {icon: 2});
            }
            
            layer.confirm('确定作废选中的会员卡？', function(index){
                var ids = checkStatus.data.map(function(item){
                    return item.id;
                });
                
                $.ajax({
                    url: '/app/ky_shouyin/admin/member/batchVoidCards',
                    type: 'POST',
                    data: {ids: ids},
                    success: function(res){
                        if(res.code === 0){
                            layer.msg('作废成功', {icon: 1});
                            table.reload('LAY-card-list');
                        } else {
                            layer.msg(res.msg || '作废失败', {icon: 2});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };
    
    $('.layui-btn-group .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
</script> 